open(BUFF,"BORA_GENDER.info");
$line=<BUFF>;
while(<BUFF>)
{
	chomp($_);
	@a=split("\t",$_);
	if($a[3] eq "Female")
	{
		$gender=2;
	}
	else
	{
		$gender=1;
	}
	$hash{$a[1]}=$gender;
	#die "$a[1]\n";
}
$dir="/data2/bsi/RandD/Arraybased_RND/tcga_genotyping";
$sdrf_dir="$dir/SRDF";
open(BUFF,"$dir/AFFY/files");
while(<BUFF>)
{
	chomp($_);
	print "$_ NORMAL\n";
	#system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal1.tfam |sort|uniq -c");
	#print "$_ TUMOR\n";
	#system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor1.tfam |sort|uniq -c");
	open(BUFF1,"/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal.tfam");
	open(WRBUFF,">/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal_build37.tfam");
	open(WRBUFF1,">/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/drop_samp_nogender.txt");
	$k=0;
	while($line=<BUFF1>)
	{
		chomp($line);
		@a=split(" ",$line);
		#die "$a[1]\n";
		@b=split("-",$line);
		#die "$b[0]-$b[1]-$b[2]\n";
		if(exists($hash{"$b[0]-$b[1]-$b[2]"}))
		{
			#die "success\n";
			$a[4]=$hash{"$b[0]-$b[1]-$b[2]"};
			$line=join(" ",@a);
		}
		if($a[4] eq "0")
        	{
			$k++;
                	#print "$_ $line\n";
                	print WRBUFF1 "$line\n";
        	}

		print WRBUFF $line."\n";
	}
	if($k>0)
        {
                system("plink --tfile /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal_build37 --remove /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/drop_samp_nogender.txt --transpose --recode --out /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal_build37_updated_gender");
	}
	#system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal.tfam |sort|uniq -c");
	#system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/NORMAL/fwdStrandResults_all_merged_removeddup_normal1.tfam |sort|uniq -c");
	print "$_ TUMOR\n";
	open(BUFF1,"/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor.tfam");
        open(WRBUFF,">/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor_build37.tfam");
        open(WRBUFF1,">/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/drop_samp_nogender.txt");
	$k=0;
	while($line=<BUFF1>)
        {
                chomp($line);
                @a=split(" ",$line);
		@b=split("-",$line);
                if(exists($hash{"$b[0]-$b[1]-$b[2]"}))
                {
                        $a[4]=$hash{"$b[0]-$b[1]-$b[2]"};
                        $line=join(" ",@a);
                }
		if($a[4] eq "0")
        	{
			$k++;
                	#print "$_ $line\n";
                	print WRBUFF1 "$line\n";
        	}
		
                print WRBUFF $line."\n";
        }
	if($k>0)
	{
		system("plink --tfile /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor_build37 --remove /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/drop_samp_nogender.txt --transpose --recode --out /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor_build37_updated_gender"); 
	}
	 #system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor.tfam |sort|uniq -c");
	#system("cut -f5 -d ' ' /data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/$_/TUMOR/fwdStrandResults_all_merged_removeddup_tumor1.tfam |sort|uniq -c");
}
